package com.donggua.aichebao.commonui.CustomTextView;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.donggua.aichebao.commonui.R;

/**
 * Created by mk on 2017-08-27.
 */
public class TextViewCornerIcon extends LinearLayout {

    private static final int DEFAULT_SIZE = 15;
    private static final int DEFAULT_TEXT_COLOR = 0Xff000000;
    private TextView mTextView;
    private ImageView mIconView;
    private boolean selected;

    public TextViewCornerIcon(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        View view= LayoutInflater.from(context).inflate(R.layout.textview_cornericon, this);
        mTextView = view.findViewById(R.id.text_view);
        mIconView = view.findViewById(R.id.corner_icon);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TextViewCornerIcon);
        selected = ta.getBoolean(R.styleable.TextViewCornerIcon_ct_ci_selected,false);
        mTextView.setText(ta.getString(R.styleable.TextViewCornerIcon_android_text));
        mTextView.setTextColor(ta.getColor(R.styleable.TextViewCornerIcon_android_textColor, DEFAULT_TEXT_COLOR));
        mTextView.setTextSize(ta.getDimension(R.styleable.TextViewCornerIcon_android_textSize, DEFAULT_SIZE));
        int src = ta.getResourceId(R.styleable.TextViewCornerIcon_android_src, R.mipmap.ic_launcher);
        if (src != -1) {
            mIconView.setImageResource(src);
        }

        if(selected){
            mIconView.setVisibility(VISIBLE);
        }else
        {
            mIconView.setVisibility(GONE);
        }

        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                selected = selected == true ? false:true;
                if(selected){
                    mIconView.setVisibility(VISIBLE);
                }else
                {
                    mIconView.setVisibility(GONE);
                }
            }
        });
    }

    public boolean getSelectStatus(){
        return selected;
    }
}
